package com.suqiu.qqrobot.controller;

import com.suqiu.qqrobot.common.result.Result;
import love.forte.simbot.Identifies;
import love.forte.simbot.application.Application;
import love.forte.simbot.application.BotManagers;
import love.forte.simbot.bot.BotManager;
import love.forte.simbot.component.mirai.bot.MiraiBot;
import love.forte.simbot.component.mirai.bot.MiraiBotManager;
import love.forte.simbot.definition.Group;
import love.forte.simbot.message.Image;
import love.forte.simbot.message.ResourceImage;
import love.forte.simbot.resources.ByteArrayResource;
import love.forte.simbot.resources.FileResource;
import love.forte.simbot.resources.Resource;
import love.forte.simbot.resources.StandardResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * @author: suqiu
 * @desc: 发送消息（非监听）
 * @date: 2023/4/17
 */
@RestController
@RequestMapping("/qq/message")
public class MessageController {

//    @Autowired
    private Application application;

    @PostMapping("/send")
    public Result<?> sendMessage(String message) throws URISyntaxException, IOException {
        BotManagers botManagers = application.getBotManagers();
        // 如果只有一种bot，也可以考虑使用 botManagers.get(0).all().get(0);
        // 这样的话注意处理异常情况
        for (BotManager<?> botManager : botManagers) {
            if (botManager instanceof MiraiBotManager) {
                // 拿到bot，怎么操作看你心情，比如往某个群发消息
                MiraiBot bot = ((MiraiBotManager) botManager).get(Identifies.ID(859882153));
                assert bot != null;
                // 给每个群组发送
//                Items<MiraiGroup> groups = bot.getGroups();
//                groups.asStream().forEach(n -> {
//                    n.sendAsync(message);
//                });
                Group group = bot.getGroup(Identifies.ID(592408407)); // warn: nullable
                assert group != null;
                group.sendBlocking(message);
                break;
            }
        }
        return Result.ok();
    }
}
